"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _jsxRuntime = require("react/jsx-runtime");

var _react = require("react");

var _proProvider = require("@ant-design/pro-provider");

var _antdV = require("antd-v4");

var _omit = _interopRequireDefault(require("omit.js"));

/**
 * FormFooter 的组件，可以自动进行一些配置
 *
 * @param props
 */
var Submitter = function Submitter(props) {
  var intl = (0, _proProvider.useIntl)();

  if (props.render === false) {
    return null;
  }

  var form = props.form,
      onSubmit = props.onSubmit,
      render = props.render,
      onReset = props.onReset,
      _props$searchConfig = props.searchConfig,
      searchConfig = _props$searchConfig === void 0 ? {} : _props$searchConfig,
      submitButtonProps = props.submitButtonProps,
      _props$resetButtonPro = props.resetButtonProps,
      resetButtonProps = _props$resetButtonPro === void 0 ? {} : _props$resetButtonPro;

  var submit = function submit() {
    form.submit();
    onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit();
  };

  var reset = function reset() {
    form.resetFields();
    onReset === null || onReset === void 0 ? void 0 : onReset();
  };

  var _searchConfig$submitT = searchConfig.submitText,
      submitText = _searchConfig$submitT === void 0 ? intl.getMessage('tableForm.submit', '提交') : _searchConfig$submitT,
      _searchConfig$resetTe = searchConfig.resetText,
      resetText = _searchConfig$resetTe === void 0 ? intl.getMessage('tableForm.reset', '重置') : _searchConfig$resetTe;
  /** 默认的操作的逻辑 */

  var dom = [];

  if (resetButtonProps !== false) {
    dom.push( /*#__PURE__*/(0, _react.createElement)(_antdV.Button, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (0, _omit.default)(resetButtonProps, ['preventDefault'])), {}, {
      key: "rest",
      onClick: function onClick(e) {
        var _resetButtonProps$onC;

        if (!(resetButtonProps === null || resetButtonProps === void 0 ? void 0 : resetButtonProps.preventDefault)) reset();
        resetButtonProps === null || resetButtonProps === void 0 ? void 0 : (_resetButtonProps$onC = resetButtonProps.onClick) === null || _resetButtonProps$onC === void 0 ? void 0 : _resetButtonProps$onC.call(resetButtonProps, e);
      }
    }), resetText));
  }

  if (submitButtonProps !== false) {
    dom.push( /*#__PURE__*/(0, _react.createElement)(_antdV.Button, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      type: "primary"
    }, (0, _omit.default)(submitButtonProps || {}, ['preventDefault'])), {}, {
      key: "submit",
      onClick: function onClick(e) {
        var _submitButtonProps$on;

        if (!(submitButtonProps === null || submitButtonProps === void 0 ? void 0 : submitButtonProps.preventDefault)) submit();
        submitButtonProps === null || submitButtonProps === void 0 ? void 0 : (_submitButtonProps$on = submitButtonProps.onClick) === null || _submitButtonProps$on === void 0 ? void 0 : _submitButtonProps$on.call(submitButtonProps, e);
      }
    }), submitText));
  }

  var renderDom = render ? render((0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
    submit: submit,
    reset: reset
  }), dom) : dom;

  if (!renderDom) {
    return null;
  }

  if (Array.isArray(renderDom)) {
    if ((renderDom === null || renderDom === void 0 ? void 0 : renderDom.length) < 1) {
      return null;
    }

    if ((renderDom === null || renderDom === void 0 ? void 0 : renderDom.length) === 1) {
      return renderDom[0];
    }

    return (0, _jsxRuntime.jsx)(_antdV.Space, {
      wrap: true,
      children: renderDom
    });
  }

  return renderDom;
};

var _default = Submitter;
exports.default = _default;